package com.ruoyi.project.module.shao;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.List;
import java.util.Date;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 消费详细Controller
 *
 * @author dr
 * @date 2023-04-17
 */
@RestController
@RequestMapping("/shao/details")
public class ShaoYqgConsumptionDetailsController extends BaseController
{
    @Autowired
    private IYqgConsumptionDetailsService yqgConsumptionDetailsService;
    @Autowired
    private IYqgInformationProjectService yqgInformationProjectService;
    @Autowired
    private IYqgHandsService yqgHandsService;
    @Autowired
    private IYqgBaseinfoService yqgBaseinfoService;
    @Autowired
    private IYqgBarBillsService yqgBarBillsService;
    @Autowired
    private IYqgItemInventoryService yqgItemInventoryService;

    /**
     * 查询消费详细列表
     */
//    @RequiresPermissions("project:details:list")
    @GetMapping("/list")
    public TableDataInfo list(YqgConsumptionDetails yqgConsumptionDetails)
    {
        startPage();
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        return getDataTable(list);
    }
    /**
     * 查询浴袍
     */
    @GetMapping("/listyupao")
    public TableDataInfo listyupao(YqgConsumptionDetails yqgConsumptionDetails)
    {
        startPage();
        yqgConsumptionDetails.setProjectCode("40101");
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        return getDataTable(list);
    }
    /**
     * 定时出库接口
     * 这里写的方法,需要将数据库中项目信息表中的货余量字段赋值比如初始都赋值为0否则会报错
     */
//    @RequiresPermissions("project:details:list")
    @GetMapping("/listdingshi")
    public TableDataInfo listchu(YqgConsumptionDetails yqgConsumptionDetails)
    {
        // 创建 Calendar 对象
        Calendar calendar = Calendar.getInstance();
        Date date=new Date();
        calendar.setTime(date);
        // 在 Calendar 对象中增加一天
        calendar.add(Calendar.DAY_OF_YEAR, 1);
        // 获取增加一天后的日期
        Date nextDay = calendar.getTime();
        String time=DateUtils.parseDateToStr("yyyy-MM-dd",date);
        // String nexttime=DateUtils.parseDateToStr("yyyy-MM-dd",nextDay);
        yqgConsumptionDetails.setNote1(time);
        //在数据库中查询有关出库商品这里只限商品
        yqgConsumptionDetails.setProjectCategory("04");
        List<YqgConsumptionDetails> list04 = yqgConsumptionDetailsService.ShaoselectYqgConsumptionDetailsList(yqgConsumptionDetails);
        yqgConsumptionDetails.setProjectCategory("05");
        List<YqgConsumptionDetails> list05 = yqgConsumptionDetailsService.ShaoselectYqgConsumptionDetailsList(yqgConsumptionDetails);
        list04.addAll(list05);
        yqgConsumptionDetails.setProjectCategory("07");
        List<YqgConsumptionDetails> list07 = yqgConsumptionDetailsService.ShaoselectYqgConsumptionDetailsList(yqgConsumptionDetails);
        list04.addAll(list07);
        yqgConsumptionDetails.setProjectCategory("08");
        List<YqgConsumptionDetails> list08 = yqgConsumptionDetailsService.ShaoselectYqgConsumptionDetailsList(yqgConsumptionDetails);
        list04.addAll(list08);
        //将商品放入list04数组中
        //新建个yqgInformationProject对象用于操作该表因为是要改货余量
        YqgInformationProject yqgInformationProject=new YqgInformationProject();
        //遍历该数组查询当天消费的每个商品的数量
        for(int i=0;i<list04.size();i++){
                //将要出库的商品的数量赋值给c
                int c =Integer.parseInt(list04.get(i).getQuantity());
                //在项目信息表中通过ProjectCode查看对应项目的货余量
                yqgInformationProject.setProjectCode(list04.get(i).getProjectCode());
                List<YqgInformationProject> danlist = yqgInformationProjectService.selectYqgInformationProjectList(yqgInformationProject);
                //将货余量赋值给a
                int a=Integer.parseInt(danlist.get(0).getCargoAllowance());
                //用a-c得到b就是剩余之后的货余量之后赋值到CargoAllowance字段中
                int b=a-c;
                if(b>0){
                    //如果b为正数那么就证明还有此货物所以正常赋值即可
                    danlist.get(0).setCargoAllowance(String.valueOf(b));
                }else {
                    //否则直接将其置为0
                    danlist.get(0).setCargoAllowance("0");
                }
                //进行修改,修改YqgInformationProject这个表
                yqgInformationProjectService.updateYqgInformationProject(danlist.get(0));
                //新建一个yqgItemInventory对象用于操作这个表用于将一些信息添加到此表中
                YqgItemInventory yqgItemInventory=new YqgItemInventory();
                //登录获得员工的nickname
                yqgItemInventory.setOutboundPerson("系统定时出库");
                //获得项目名称
                yqgItemInventory.setEntryName(list04.get(i).getEntryName());
                //项目代码
                yqgItemInventory.setProjectCode(list04.get(i).getProjectCode());
                //出库日期
                yqgItemInventory.setDeliveryTime(DateUtils.getNowDate());
                //货余量
                if(b>0){
                    yqgItemInventory.setCargoAllowance(String.valueOf(b));
                }
                else {
                    yqgItemInventory.setCargoAllowance("0");
                }
                //出库数量
                yqgItemInventory.setQuantityDelivered(list04.get(i).getQuantity());
                yqgItemInventory.setNote3("自动出库");
                //新增到YqgItemInventory表中
                yqgItemInventoryService.insertYqgItemInventory(yqgItemInventory);
            }
        return getDataTable(list04);
    }

    /**
     * 导出消费详细列表
     */
//    @RequiresPermissions("project:details:export")
    @Log(title = "消费详细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YqgConsumptionDetails yqgConsumptionDetails)
    {
        List<YqgConsumptionDetails> list = yqgConsumptionDetailsService.selectYqgConsumptionDetailsList(yqgConsumptionDetails);
        ExcelUtil<YqgConsumptionDetails> util = new ExcelUtil<YqgConsumptionDetails>(YqgConsumptionDetails.class);
        util.exportExcel(response, list, "消费详细数据");
    }

    /**
     * 获取消费详细详细信息
     */
//    @RequiresPermissions("project:details:query")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        return success(yqgConsumptionDetailsService.selectYqgConsumptionDetailsByID(ID));
    }

    /**
     * 新增消费详细
     */
//    @RequiresPermissions("project:details:add")
    @Log(title = "消费详细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {

        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //通过登录查询相关服务员信息
        yqgConsumptionDetails.setUserId(sysUser.getUserId());
        yqgConsumptionDetails.setDeptId(sysUser.getDeptId());
        yqgConsumptionDetails.setNickName(sysUser.getNickName());
        yqgConsumptionDetails.setUserName(sysUser.getUserName());
        yqgConsumptionDetails.setPost(sysUser.getRemark());
        YqgBaseinfo yqgBaseinfo= new YqgBaseinfo();
        yqgBaseinfo.setUserId(loginUser.getUserid());
        List<YqgBaseinfo> list = yqgBaseinfoService.selectYqgBaseinfoList(yqgBaseinfo);
        for(YqgBaseinfo baseinfo: list )
        {
            yqgConsumptionDetails.setDepartment(baseinfo.getDepartment());

        }
        //通过消费详细表中的商品在项目信息表查询其相关信息并存入数据库
        YqgInformationProject yqgInformationProject=new YqgInformationProject();
        yqgInformationProject.setEntryName(yqgConsumptionDetails.getEntryName());
        List<YqgInformationProject> Plist = yqgInformationProjectService.selectYqgInformationProjectList(yqgInformationProject);
        for(YqgInformationProject InformationProject:Plist){
            yqgConsumptionDetails.setProjectCode(InformationProject.getProjectCode());
            yqgConsumptionDetails.setMnemonicCode(InformationProject.getMnemonicCode());
            yqgConsumptionDetails.setUnit(InformationProject.getUnit());
            yqgConsumptionDetails.setUnitPrice(InformationProject.getUnitPrice());
            yqgConsumptionDetails.setCommissionPrice(InformationProject.getCommissionPrice());
        }
            yqgConsumptionDetails.setRecordingTime(DateUtils.getNowDate());
        //通过吧台表中的手牌来获取整个吧台表中的信息
        YqgBarBills yqgBarBills = new YqgBarBills();
        yqgBarBills.setHandBrand(yqgConsumptionDetails.getHandBrand());
        yqgBarBills.setHandState(yqgConsumptionDetails.getHandState());
        List<YqgBarBills> batailist = yqgBarBillsService.selectYqgBarBillsList(yqgBarBills);
        for (YqgBarBills barBills:batailist){
            yqgConsumptionDetails.setBarBillsId(barBills.getID());
            yqgConsumptionDetails.setHandCategory(barBills.getHandCategory());
            yqgConsumptionDetails.setOpeningTime(DateUtils.getNowDate());
            yqgConsumptionDetails.setMergeStatus(barBills.getMergeStatus());
            yqgConsumptionDetails.setRetentionStatus(barBills.getRetentionStatus());
            yqgConsumptionDetails.setTogether(barBills.getTogether());
            yqgConsumptionDetails.setOriginalHand(barBills.getOriginalHand());
            yqgConsumptionDetails.setHandoverStatus(barBills.getHandoverStatus());
        }
        // 获取当前登录用户
        yqgConsumptionDetails.setUserName(loginUser.getUsername());
        yqgConsumptionDetails.setUserId(loginUser.getUserid());
        //将部门id注入到信息表中
        yqgBaseinfo.setUserId(loginUser.getUserid());
        List<YqgBaseinfo> listbumen = yqgBaseinfoService.selectYqgBaseinfoList(yqgBaseinfo);
        for(YqgBaseinfo baseinfo:listbumen)
        {
            yqgConsumptionDetails.setDeptId(baseinfo.getDeptId());
            yqgConsumptionDetails.setNickName(baseinfo.getNickName());
        }
        //已点商品的商品状态字段全变成0
        yqgConsumptionDetails.setConsumptionStatus("0");
        yqgConsumptionDetails.setDepartment("2");

        return toAjax(yqgConsumptionDetailsService.insertYqgConsumptionDetails(yqgConsumptionDetails));

    }

    /**
     * 修改消费详细
     */
//    @RequiresPermissions("project:details:edit")
    @Log(title = "消费详细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YqgConsumptionDetails yqgConsumptionDetails)
    {
        return toAjax(yqgConsumptionDetailsService.updateYqgConsumptionDetails(yqgConsumptionDetails));
    }

    /**
     * 删除消费详细
     */
//    @RequiresPermissions("project:details:remove")
    @Log(title = "消费详细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        return toAjax(yqgConsumptionDetailsService.deleteYqgConsumptionDetailsByIDs(IDs));
    }
}
